package com.atguigui.leetcode;

import java.util.Arrays;

/**
 * 1760.袋子里最少数目的球
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/12/20 9:31
 */
public class P1760MinimumLimitOfBallsInABag {

    public static void main(String[] args) {
        Solution solution = new P1760MinimumLimitOfBallsInABag().new Solution();
        // TO TEST
    }

    class Solution {
        /**
         * 二分法
         *
         * @param nums
         * @param maxOperations 操作次数
         * @return
         */
        public int minimumSize(int[] nums, int maxOperations) {
            int left = 1, right = Arrays.stream(nums).max().getAsInt();
            int ans = 0;
            while (left <= right) {
                int y = (left + right) / 2;
                //操作次数
                long ops = 0;
                for (int x : nums) {
                    ops += (x - 1) / y;
                }
                //判断是否小于操作次数
                if (ops <= maxOperations) {
                    ans = y;
                    right = y - 1;
                } else {
                    left = y + 1;
                }
            }
            return ans;
        }
    }
}
